package com.hougong.im.transport;


import com.hougong.im.Dispather;
import com.hougong.im.pool.ConnPool;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 最终接收数据的业务Handler ，可能会有树多个判断 分支
 * 1.根据不同的业务提取命令  做判断 获取硬件信息，
 * 2.再按照协议组装不同的消息，然后传给编码器，发送二进制数据
 *
 * @author Yohann.
 */
public class AcceptHandler extends ChannelInboundHandlerAdapter {
    private static final Logger LOGGER=LoggerFactory.getLogger(AcceptHandler.class);
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        if (msg instanceof BaseFrame) {
            BaseFrame baseFrame= (BaseFrame) msg;
            Dispather.handler(baseFrame,ctx.channel());
        } else {
            throw new IllegalArgumentException("msg is not instance of MessageHolder");
        }
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        LOGGER.info(ctx.channel().remoteAddress()+"--------连接到TCP服务器----");
        super.channelActive(ctx);
    }


    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        super.exceptionCaught(ctx, cause);
    }

}
